﻿//===========================[ 8 / 9 / 2011 LVI ]=============================
//				Copyright c Vitaly Lyaschenko <SkyChameleon@gmail.com>
//								Source file
// Purpose: конвертация *.3DS -> *.IFM 
// просто перетащить *.3ds файл на 3dsToIfm.exe :) -> результат *.ifm
//=============================================================================

#include "common.h"
#include "cwLogger.h"
#include "cMeshManager.h"
#include "cFileManager.h"

cFileManager   *pFileManager = NULL;
cMeshesManager *pMeshManager = NULL;
cwLogger       *logger       = NULL;

int main( int argc, char **argv )
{
	logger = new cwLogger;
	cwLogger::create("Convert.log");

	pFileManager = new cFileManager;
	pMeshManager = new cMeshesManager;

	// если был просто запуск *.exe
	if ( argv[1] == NULL )
	{
		std::cout << "\n  Please DRAG and DROP *.3ds file on .EXE ! \n";
		std::cout << "\n  Try again...\n\n";
		cwLogger::writeERROR( "  Input ERROR !\n" );
		system("PAUSE");
		return -1;
	}

	std::string strBuff;

	// меняем обратный слеш на православный :)
	int len = 0;
	len = strlen(argv[1]);
	for( int i = 0; i < len;)
	{
		bool f = false;
		if( argv[1][i] == '\\' )
		{
			strBuff.push_back( '/' );
			++i;
			f = true;
		}
		if( f )
			continue;
		strBuff.push_back( argv[1][i] );
		++i;
	}
	std::string ext( cFileManager::extractFileExtension(strBuff) );
	// если расширения совпадают
	if( ( ext == ".ifm" ) || (ext == ".IFM" ) )
		exit(0);

	cwLogger::writeINFO( "Converted Mesh : " + strBuff );
	std::string nameMesh = cFileManager::extractFileNameEx( strBuff );
	std::cout <<std::endl<< "  --| INTERNAL FORMAT MODEL CONVERTER |-- \n\n";
	std::cout << "  <+> Converted Mesh File : " << nameMesh<<"\n\n";

	cMesh *mesh;
	mesh = new cMesh;
	std::cout<<"  <+> Load mesh... \n\n";
	mesh = cMeshesManager::loadMesh( nameMesh );

	std::cout<<"  <+> Optimize mesh... \n\n";
	cMeshesManager::optimize( mesh, WELD_VERTICES );

	std::cout<<"  <+> Save mesh... \n\n";
	cMeshesManager::saveMesh( nameMesh, mesh );

	std::cout<<"  --| D O N E ! |-- \n\n";
	system("PAUSE");

	// free memory
	SAFE_DELETE( mesh );
	SAFE_DELETE( logger );
	SAFE_DELETE( pFileManager );
	SAFE_DELETE( pMeshManager );

return 0;
}